package com.kd.crawler.dao;

import com.kd.crawler.common.utils.StringUtils;
import com.kd.crawler.entity.CrawlerEntry;
import com.kd.crawler.entity.Document;

public class StatisticEntryUrlDao extends BaseDao {

	private static StatisticEntryUrlDao instance = new StatisticEntryUrlDao();
	
	private StatisticEntryUrlDao() {
		
	}
	
	public static StatisticEntryUrlDao getInstance() {
		return instance;
	}
	
	public int insert(CrawlerEntry ce, int innerLinksNumber, String analyzerFlag) {
		String sql = "insert into tb_statistics_for_entry_url(job_id, entry_id, inner_links_sum, " +
				" title_success_sum, title_failed_sum, date_success_sum, date_failed_sum, author_success_sum, author_failed_sum, " +
				" content_success_sum, content_failed_sum, analyzer_flag, create_time) " +
				" values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now())";
		int result = -1;
		try {
			result = this.update(sql, ce.getJobID(), ce.getEntryId(), innerLinksNumber, 0, 0, 0, 0, 0, 0, 0, 0, analyzerFlag);
		} catch(Exception e) {
			result = -1;
			e.printStackTrace();
		}
		return result;
	}
	
	public int insert(CrawlerEntry ce, Document doc, int innerLinksNumber, String analyzerFlag) {
		String sql = "insert into tb_statistics_for_entry_url(job_id, entry_id, inner_links_sum, " +
				" title_success_sum, title_failed_sum, date_success_sum, date_failed_sum, author_success_sum, author_failed_sum, " +
				" content_success_sum, content_failed_sum, analyzer_flag, create_time) " +
				" values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now())";
		int titleSuccess = 0;
		int titleFailed = 0;
		int dateSuccess = 0;
		int dateFailed = 0;
		int authorSuccess = 0;
		int authorFailed = 0;
		int contentSuccess = 0;
		int contentFailed = 0;
		if(StringUtils.isBlank(doc.getTitle())) {
			titleFailed = 1;
		} else {
			titleSuccess = 1;
		}
		if(null ==doc.getDate()) {
			dateFailed = 1;
		} else {
			dateSuccess = 1;
		}
		if(StringUtils.isBlank(doc.getAuthor())) {
			authorFailed = 1;
		} else {
			authorSuccess = 1;
		}
		if(StringUtils.isBlank(doc.getContent())) {
			contentFailed = 1;
		} else {
			contentSuccess = 1;
		}
		try {
			return this.update(sql, ce.getJobID(), ce.getEntryId(), innerLinksNumber, titleSuccess, titleFailed, dateSuccess, dateFailed, authorSuccess, authorFailed, contentSuccess, contentFailed, analyzerFlag);
		} catch(Exception e) {
			e.printStackTrace();
			return -1;
		}
	}
	
	public int update(CrawlerEntry ce, Document doc, String analyzerFlag) {
		String sql = "update tb_statistics_for_entry_url set inner_links_sum=inner_links_sum+1, title_success_sum=title_success_sum+?, title_failed_sum=title_failed_sum+?, " +
				"date_success_sum=date_success_sum+?, date_failed_sum=date_failed_sum+?, author_success_sum=author_success_sum+?, author_failed_sum=author_failed_sum+?, " + 
				"content_success_sum=content_success_sum+?, content_failed_sum=content_failed_sum+? where entry_id=? and analyzer_flag=?";
		int titleSuccess = 0;
		int titleFailed = 0;
		int dateSuccess = 0;
		int dateFailed = 0;
		int authorSuccess = 0;
		int authorFailed = 0;
		int contentSuccess = 0;
		int contentFailed = 0;
		if(StringUtils.isBlank(doc.getTitle())) {
			titleFailed = 1;
		} else {
			titleSuccess = 1;
		}
		if(null ==doc.getDate()) {
			dateFailed = 1;
		} else {
			dateSuccess = 1;
		}
		if(StringUtils.isBlank(doc.getAuthor())) {
			authorFailed = 1;
		} else {
			authorSuccess = 1;
		}
		if(StringUtils.isBlank(doc.getContent())) {
			contentFailed = 1;
		} else {
			contentSuccess = 1;
		}
		try {
			return this.update(sql, titleSuccess, titleFailed, dateSuccess, dateFailed, authorSuccess, authorFailed, contentSuccess, contentFailed, ce.getEntryId(), analyzerFlag);
		} catch(Exception e) {
			e.printStackTrace();
			return -1;
		}
	}
	
	public Long getLong(int entryId, String analyzerFlag) {
		String sql = "select 1 from tb_statistics_for_entry_url where entry_id=? and analyzer_flag=?";
		return this.getLong(sql, entryId, analyzerFlag);
	}
	
	public static void main(String[] args) {
		System.out.println(StatisticEntryUrlDao.getInstance().getLong(554, "O"));
	}
}
